package arch.dao.impl.user.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import arch.domain.user.Dept;
import arch.domain.user.User;
import arch.util.SecretPasswd;
/**
 * UserMapper<br/>
 * @author ZhaoWangLong Sep 3, 2012
 *
 */
public class UserMapper implements RowMapper {
	private boolean defaultDept;
	
	public UserMapper(){}
	
	public UserMapper(boolean defaultDept){
		this.defaultDept = defaultDept;
	}
	
	public Object mapRow(ResultSet rs, int i) throws SQLException {
		User u = new User();
		u.setUserId(rs.getString("USER_ID"));
		u.setUserName(rs.getString("USER_NAME"));
		u.setPassword(SecretPasswd.removeSecret(rs.getString("PASSWORD")));
		u.setLock("1".equals(rs.getString("IS_LOCK"))?true:false);
		u.setErrorCount(rs.getInt("ERROR_COUNT"));
		
		Dept defaultLoginDept = new Dept();
		defaultLoginDept.setDeptId(rs.getString("DEFAULT_LOGIN_DEPT"));
		if(defaultDept){
			defaultLoginDept.setDeptName(rs.getString("DEFAULT_LOGIN_DEPT_NAME"));
			defaultLoginDept.setDeptCode(rs.getString("DEFAULT_LOGIN_DEPT_CODE"));
		}
		u.setDefaultLoginDept(defaultLoginDept);
		
		u.setCreateTime(rs.getTimestamp("CREATE_TIME"));
		u.setLastUpdateTime(rs.getTimestamp("LAST_UPDATE_TIME"));
		u.setLastUpdatePasswdTime(rs.getTimestamp("LAST_UPDATE_PASSWD_TIME"));
		return u;
	}

}
